package com.ctl.controller;

import com.alibaba.fastjson.JSON;
import com.ctl.dto.VideoSearchDTO;
import com.ctl.dto.VideosDTO;
import com.ctl.entity.User;
import com.ctl.entity.Video;
import com.ctl.feign.UserFeign;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SearhController {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private UserFeign userFeign;

    @GetMapping("/search/videos")
    public VideoSearchDTO searchVideo(String q,
                                            @RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer per_page){
        //创建es请求客户端对象
        SearchRequest searchRequest = new SearchRequest();
        //创建es查询对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.from((page-1)*per_page)
                          .size(per_page) //分页查询
                .query(QueryBuilders.termQuery("title",q)); //查询条件
        //设置查询索引
        searchRequest.indices("videos").searchType("video").source(searchSourceBuilder);
        //创建与es客户端的连接
        try {
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            //获取es查询到数据
            SearchHits hits = searchResponse.getHits();
            //获取查询到的总条数
            VideoSearchDTO videoSearchDTO = new VideoSearchDTO();
            videoSearchDTO.setTotal_count(hits.getTotalHits());
            ArrayList<VideosDTO> videosDTOS = new ArrayList<>();
            //获取查询到的单个视频对象
            SearchHit[] hitsv = hits.getHits();
            for (SearchHit fields : hitsv) {
                //es查询到的对象转换
                String esString = fields.getSourceAsString();
                    //将转换的数据通过JSON转换成 视频 video对象
                Video video = JSON.parseObject(esString, Video.class);
                VideosDTO videosDTO = new VideosDTO();
                //获取视频用户
                BeanUtils.copyProperties(video,videosDTO);
                User user = userFeign.getUser(video.getUid());
                videosDTO.setUploader(user.getName());
                videosDTOS.add(videosDTO);
            }
            videoSearchDTO.setItems(videosDTOS);
            return videoSearchDTO;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
